package com.walker.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;

public class CommonUtils {
	
	public static String valueOf(Object object){
		String str = StringUtils.trim(String.valueOf(object));
		boolean check = StringUtils.isBlank(str) || StringUtils.equals(str, "null") || StringUtils.equals(str, "undefined");
		return check ? "" : str;
	}
	
	public static boolean isNull(Object object){
		String str = StringUtils.trim(String.valueOf(object));
		return StringUtils.isBlank(str) || StringUtils.equals(str, "null") || StringUtils.equals(str, "undefined");
	}
	
	/**
	 *单位自适应 
	 * @param data：原始单位B
	 * @return
	 */
	public static String formateRawSize(String raw_size){
		try{
			double data = 0;
			if(StringUtils.isBlank(raw_size)){
				return raw_size;
			}
			data = Double.parseDouble(raw_size);
			
			DecimalFormat df = new DecimalFormat("#.##");
			int count = 0;
		    String result = "";
		    
		    while(data >= 1024){
		    	data = data / 1024;
		    	count ++;
		    }
		    result = df.format(data).replace(".0","");
			switch (count) {
				case 0:
					result = result + "B";
					break;
				case 1:
					result = result + "KB";
					break;
				case 2:
					result = result + "MB";
					break;
				case 3:
					result = result + "GB";
					break;
				case 4:
					result = result + "TB";
					break;
				case 5:
					result = result + "PB";
					break;
				case 6:
					result = result + "EB";
					break;
				case 7:
					result = result + "ZB";
					break;
			}
			return result;
		}catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
	
	public static boolean isValidDate(String date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try{
			sdf.setLenient(false);//严格按照给定的格式来校验
			sdf.parse(date);
			return true;
		}catch(Exception e){
			System.out.println(e.getMessage());
			return false;
		}
	}
	
	/**
	 * 转换为Integer类型
	 */
	public static Integer toInteger(Object val){
		return toLong(val).intValue();
	}
	
	/**
	 * 转换为Long类型
	 * @param val
	 * @return
	 */
	public static Long toLong(Object val){
		return toDouble(val).longValue();
	}
	
	/**
	 * 转换为Double类型
	 * @param val
	 * @return
	 */
	public static Double toDouble(Object val){
		if (val == null){
			return -999D;
		}
		try {
			return Double.valueOf(valueOf(val));
		} catch (Exception e) {
			return -999D;
		}
	}
	
    public static long ip2Long(String ip) {
		String[] ips = ip.split("[.]");
		long num = Long.parseLong(ips[0]) << 24;
		num += Long.parseLong(ips[1]) << 16;
		num += Long.parseLong(ips[2]) << 8;
		num += Long.parseLong(ips[3]);
		return num;
	}
	
	public static long ipToLong(String ip){
		int pos1 = ip.indexOf(".");
		int pos2 = ip.indexOf(".",pos1+1);
		int pos3 = ip.indexOf(".",pos2+1);
		long num = Long.parseLong(ip.substring(0,pos1)) << 24;
		num += Long.parseLong(ip.substring(pos1+1,pos2)) << 16;
		num += Long.parseLong(ip.substring(pos2+1,pos3)) << 8;		
		num += Long.parseLong(ip.substring(pos3+1));
		return num;
	}

	public static String long2IP(long longIp) {
		StringBuffer sb = new StringBuffer("");
		sb.append(String.valueOf((longIp >>> 24)));
		sb.append(".");
		sb.append(String.valueOf((longIp & 0x00FFFFFF) >>> 16));
		sb.append(".");
		sb.append(String.valueOf((longIp & 0x0000FFFF) >>> 8));
		sb.append(".");
		sb.append(String.valueOf((longIp & 0x000000FF)));
		return sb.toString();
	}
	
	public static String arrayToStr(String[] array){
		String str="";
		for(String s:array){
			str=str+s+",";
		}
		str=str.substring(0,str.length()-1);
		return str;
	}
	
	/**
	 * 判断当前操作系统是不是window
	 * 
	 * @return boolean
	 */
	public static boolean isWindows() {
		boolean flag = false;
		if (System.getProperties().getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1) {
			flag = true;
		}
		return flag;
	}
	
	public static String generateId(){
		return UUID.randomUUID().toString().replaceAll("-", "");
	}
}
